package com.yuting.code.service;

import com.yuting.code.dao.CodeDao;
import com.yuting.common.pojo.AuthCode;
import lagou.service.dubbo.api.RpcCodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class RpcCodeServiceImpl implements RpcCodeService {
    @Autowired
    private CodeDao codeDao;
    @Override
    public Integer codeValidate(String email, String code) {
        List<AuthCode> all = codeDao.findAllByEmailEqualsAndCodeEquals(email, code);
        // 若找不到该记录，则返回1
        if(all.isEmpty()){
            log.info("找不到email-{}, code-{}",email, code);
            return 1;
        }

        //过滤掉过期的记录
        List<AuthCode> filtered = all.stream()
                .filter(authCode -> authCode.getExpireTime().after(new Date()))
                .collect(Collectors.toList());

        //此时为空，则是超时了
        if(filtered.isEmpty()){
            log.info("过滤之后没有有效的code了！");
            return 2;
        }else{
            return 0;
        }
    }
}
